import requests
import datetime
import mysql.connector

def fetch_today_data():
    today = datetime.date.today().strftime("%Y-%m-%d")
    page = 1
    results = []
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                      "AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/105.0.0.0 Safari/537.36",
        "Referer": "https://app.chinaz.com/",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "en-US,en;q=0.9"
    }
    while True:

        url = f"https://app.chinaz.com/djflkdsoisknfoklsyhownfrlewfknoiaewf/ai/GetAiInfoList.aspx?page={page}&type=1&isen=0"
        response = requests.get(url, headers=headers)
        if response.status_code != 200:
            print(f"获取第 {page} 页数据失败，状态码：{response.status_code}")
            break

        data = response.json()
        if not data:
            print(f"第 {page} 页没有数据，结束。")
            break

        today_data_found = False
        for item in data:
            insert_data(item)
            # 判断 addtime 字段是否以当天日期开头（格式："YYYY-MM-DD ..."）
            if item.get("addtime", "").startswith(today):
                results.append({
                    "title": item.get("title", ""),
                    "summary": item.get("summary", ""),
                    'addTime': item.get("addtime", ""),
                    'description':item.get("description","")
                })
                today_data_found = True

        # 如果本页没有当天的数据，则认为后续数据也不属于当天
        if not today_data_found:
            break

        page += 1

    return results



def insert_data(article_data):


    # 数据库连接配置
    config = {
        'host': 'localhost',  # 数据库主机地址
        'user': 'root',  # 数据库用户名
        'password': '12345678',  # 数据库密码
        'database': 'aibase'  # 数据库名称
    }

    # 连接到 MySQL 数据库
    try:
        # 建立数据库连接
        connection = mysql.connector.connect(**config)
        cursor = connection.cursor()
        # 插入数据的 SQL 查询
        insert_query = """
        INSERT INTO articles (
            Id,
            title,
            subtitle,
            description,
            thumb,
            classname,
            summary,
            tags,
            sourcename,
            author,
            status,
            url,
            type,
            added,
            addtime,
            upded,
            updtime,
            isshoulu,
            vurl,
            vsize,
            weight,
            isailog,
            sites,
            categrates,
            pv
        ) VALUES (
            %(Id)s,
            %(title)s,
            %(subtitle)s,
            %(description)s,
            %(thumb)s,
            %(classname)s,
            %(summary)s,
            %(tags)s,
            %(sourcename)s,
            %(author)s,
            %(status)s,
            %(url)s,
            %(type)s,
            %(added)s,
            %(addtime)s,
            %(upded)s,
            %(updtime)s,
            %(isshoulu)s,
            %(vurl)s,
            %(vsize)s,
            %(weight)s,
            %(isailog)s,
            %(sites)s,
            %(categrates)s,
            %(pv)s
        )
        """

        Id = article_data.get("Id","")
        # 执行插入操作
        query = "SELECT COUNT(*) FROM articles WHERE id = %s"
        cursor.execute(query, (Id,))
        result = cursor.fetchone()

        # 检查结果
        if result[0] > 0:
            print(f"ID {Id} 已存在，跳过处理。")
        else:
            cursor.execute(insert_query, article_data)
            connection.commit()
            print(f"数据插入成功！{Id}")

    except mysql.connector.Error as err:
        print(f"错误：{err}")

    finally:
        # 关闭游标和数据库连接
        if connection.is_connected():
            cursor.close()
            connection.close()


if __name__ == "__main__":
    today_results = fetch_today_data()
    print('总大小是：',len(today_results))
    for r in today_results:
        print("Title:", r["title"])
        print("description:", r["description"])
        print("addTime:", r["addTime"])
        print("----------")
